﻿using MediatR;
using SlaughterItem.Common.Domains.Check_Ins;
using SlaughterItem.Common.Domains.ListWeek2;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;
using System;

namespace SlaughterItem.API.Application.Commands.Week02.LiveAnimalsa
{
    public class LiveAnimalsAddHandler : IRequestHandler<LiveAnimalsAddCommand, Result>
    {
        IBaseRepository<LiveAnimals> LiveAnimalsRep;
        IBaseRepository<CheckIn> CheckInRep;

        public LiveAnimalsAddHandler(IBaseRepository<LiveAnimals> liveAnimalsRep, IBaseRepository<CheckIn> checkInRep)
        {
            LiveAnimalsRep = liveAnimalsRep;
            CheckInRep = checkInRep;
        }
        /// <summary>
        /// 添加活畜秤重
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(LiveAnimalsAddCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var aaa = CheckInRep.GetAll().FirstOrDefault(x => x.CheckInNo == request.CheckInNo);
                var num = 0;
                if (aaa != null)
                {
                    num = 120 - Convert.ToInt32(request.LiveAnimalsWeight);
                }
                var list = LiveAnimalsRep.Add(new LiveAnimals()
                {
                    LiveAnimalsDate = request.LiveAnimalsDate,
                    LiveAnimalsDocument = request.LiveAnimalsDocument,
                    CheckInId = aaa.CheckInId,
                    LiveAnimalsWeight = request.LiveAnimalsWeight,
                    LiveAnimalsRAverageWeight = Convert.ToString(num),
                    IsDelete = false
                });

                if (list > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = null;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
